<?php

/**
 * This is the model class for table "cms".
 *
 * The followings are the available columns in table 'cms':
 * @property integer $id
 * @property string $title
 * @property string $slug
 * @property string $meta_title
 * @property string $meta_keywords
 * @property string $meta_description
 * @property string $content
 * @property string $status
 * @property string $bg_color
 * @property string $image
 * @property string $button_text
 * @property integer $button_link
 * @property string $is_datacard
 * @property string $banner_heading
 * @property string $banner_small
 * @property string $created
 * @property string $modified
 *
 * The followings are the available model relations:
 * @property Cms $buttonLink
 * @property Cms[] $cms
 * @property Menuitems[] $menuitems
 */
class Cms extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cms';
	}

	

	public function rules(){
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, slug, meta_title, content, status, created', 'required'),
			array('title, slug, meta_title, meta_description, meta_keywords, bg_color, image, banner_heading, banner_small, button_text, button_link', 'length', 'max'=>255),
			array('image', 'file', 'allowEmpty'=>true, 'types'=>'jpg, jpeg, png', 'message'=>'Jpg/jpeg/png files only', 'on'=>'insert', 'on'=>'update'),
			array('title, slug', 'unique'),
			array('status, is_datacard', 'length', 'max'=>1),
			array('meta_description, meta_keywords, modified', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, title, slug, meta_title, meta_description, meta_keywords, content, status, bg_color, image, banner_heading, banner_small, button_text, button_link, is_datacard, created, modified', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				//'buttonLink' => array(self::BELONGS_TO, 'Cms', 'button_link'),
				'cms' => array(self::HAS_MANY, 'Cms', 'button_link'),
				'menuitems' => array(self::HAS_MANY, 'Menuitems', 'page_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Title',
			'slug' => 'Slug',
			'meta_title' => 'Meta Title',
			'meta_description' => 'Meta Description',
			'meta_keywords' => 'Meta Keywords',
			'content' => 'Content',
			'status' => 'Status',
			'bg_color' => 'Bg Color',
			'image' => 'Image',
			'banner_heading' => 'Banner Heading',
			'button_text' => 'Button Text',
			'banner_small' => 'Banner small Text',
			'button_link' => 'Button Link',
			'is_datacard' => 'Is Datacard',
			'created' => 'Created',
			'modified' => 'Modified',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search(){
		// @todo Please modify the following code to remove attributes that should not be searched.
		$criteria=new CDbCriteria;
		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('slug',$this->slug,true);
		$criteria->compare('meta_title',$this->meta_title,true);
		$criteria->compare('meta_description',$this->meta_description,true);
		$criteria->compare('meta_keywords',$this->meta_keywords,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('bg_color',$this->bg_color,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('button_text',$this->button_text,true);
		$criteria->compare('button_link',$this->button_link);
		$criteria->compare('banner_heading',$this->banner_heading,true);
		$criteria->compare('banner_small',$this->banner_small,true);
		$criteria->compare('is_datacard',$this->is_datacard,true);
		$criteria->compare('modified',$this->modified,true);
		
		if ($this->scenario === 'datacard') {
			$criteria->compare('is_datacard','1',true);
		} else {
			$criteria->compare('is_datacard','0',true);
		}
		
		if ( isset($this->created) && !empty($this->created) ){
			$created = date("Y-m-d", strtotime($this->created));
			$criteria->compare('created',$created,true);
		}
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
					'defaultOrder'=>array('modified'=>true),
			),
			'pagination'=>array(
					'pageSize'=>15,
			),
		));
	}
	
	public function keywordSearch($keyword){
		$criteria = new CDbCriteria;
		
		$criteria->compare('title', $keyword, true, 'OR');
		$criteria->compare('content', $keyword, true, 'OR');
		
		return  Cms::model()->findAll($criteria);
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Cms the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
